import initial
import os


def get_person(name):
    family = initial.get_family()
    for i in family:
        if i.name == name:
            return i


def storage_str(person):
    str1 = ''
    str1 += person.name + '\n'
    str1 += person.gender + '\n'
    str1 += person.birth + '\n'
    str1 += person.marry + '\n'
    str1 += person.address + '\n'
    str1 += person.tel + '\n'
    str1 += person.qqNum + '\n'
    str1 += person.email + '\n'
    str1 += person.father.name + '\n'
    return str1


def modify_factor(person):
    str1 = storage_str(person)  # 获得修改前的字符串信息
    factor = input('请输入想要修改的信息\n>>>')
    info = input('想要修改成\n>>>')
    if factor == '婚否':
        person.marry = info
        print('修改成功！！！')
    if factor == '住址':
        person.address = info
        print('修改成功！！！')
    if factor == '电话':
        person.tel = info
        print('修改成功！！！')
    if factor == 'QQ':
        person.qqNum = info
        print('修改成功！！！')
    if factor == 'email':
        person.email = info
        print('修改成功！！！')

    str2 = storage_str(person)  # 获得修改后的字符串信息
    with open('family.txt', 'r', encoding='utf-8') as read_f, open('family.new.txt', 'w', encoding='utf-8') as write_f:
        data = read_f.read()
        print()
        data = data.replace(str1, str2)
        write_f.write(data)
    os.remove('family.txt')
    os.rename('family.new.txt.', 'family.txt')


def modify():
    name = input('请输入想要修改的人的姓名\n>>>')
    person = get_person(name)
    modify_factor(person)